package com.qisi.kandroidshalon.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.qisi.kandroidshalon.ComposeBaseDemo
import com.qisi.kandroidshalon.leetcode.binary.tree.BinaryTree
import com.qisi.kandroidshalon.leetcode.binary.tree.TreeNode

class MainActivity : ComponentActivity() {

    private val entries = mutableListOf<MainEntry>().apply {
        add(MainEntry().apply {
            name = "基础布局"
            target = ComposeBaseDemo::class.java
        })
        add(MainEntry().apply {
            name = "简单列表"
            target = LayColumDemo::class.java
        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dumpLeetCode()
        //此处是实现页面沉浸式
        enableEdgeToEdge()
        setContent {
            MainScreen(entries)
        }
        Log.i("debugTask", "" + taskId)
    }

    private fun dumpLeetCode() {
//        val prices = arrayOf(7, 1, 5, 3, 6, 4)
//        LeetCodeProfit.maxProfit(prices)
//
//        XOR.missingNumber(arrayOf(0, 1, 2, 3, 4, 5, 7))
//        val arr = arrayOf(4, 3)
//        XOR.swapNumbers(arr)


//        val singleNumber = XOR.singleNumber(arrayOf(1, 2, 1, 3, 2, 5))
//        GrayCode.grayCode(2)
//        SimpleSort.selectSort(arrayOf(5, 4, 3, 2, 1))
//        SimpleSort.insertSort(arrayOf(5, 4, 3, 2, 1))
//        Dichotomy.findPeakElement(arrayOf(1,2,1,3,5,6,4))
//        Dichotomy.binSearch(arrayOf(1, 2, 3), 2)
//        val b = Dichotomy.isPerfectSquare(Integer.MAX_VALUE)
//        Dichotomy.searchRange(arrayOf(5, 7, 7, 8, 8, 10), 6)

//        Partial.mergeSort(arrayOf(5, 4, 3, 2, 1, 0))
//        val smallSum = Partial.smallSum(arrayOf(1, 2, 4, 4, 1))
//        Log.i("Partial", "smallSum $smallSum")
//        val array = arrayOf(5, 5, 4, 4, 3, 2, 2, 2, 2, 1)
//        val array = arrayOf(6, 5, 4, 3, 2, 1)
        val array = arrayOf(6, 5, 4, 3, 1, 2, 3, 4)
//        val array = arrayOf(2,2,2,2)
//        val res = Partial.partialArray(array, 0, array.size - 1)
//        Log.i("Partial", "smallSum $res")
//        Partial.quickSort(array)
//        Partial.quickSort(array)
//        BigHeap.buildBigHeap(array)
//        Util.dumpArray(array)
//
//        BigHeap.rmAndRebuildHeap(array)
//        Util.dumpArray(array);

//        BigHeap.heapSort(array)
//        Util.dumpArray(array)
//        val bucketArr = arrayOf(16, 15, 114, 123, 11, 2, 3, 4)
//        Util.dumpArray(bucketArr)
//        RadixSort.radixSort(bucketArr)
//        val head = ListNode(3)
//        val n1 = ListNode(2)
//        val n2 = ListNode(0)
//        val n3 = ListNode(-4)
//        head.next = n1
//        n1.next = n2
//        n2.next = n3
//        n3.next = n2
//        Log.i(Util.TAG, "start")
//        KList.hasCycle(head)
//        Log.i(Util.TAG, "end")
//        Util.dumpNode(head)
//        Util.dumpNode(KList.reverseList(head))
//        val b = KList.isPalindrome(head)
//        Log.i("KNode", "$b");
//        Util.dumpNode(head)

        //分组逆序
//        val head = ListNode(1)
//        Util.addNode(head, 2)
//        Util.addNode(head, 3)
//        Util.addNode(head, 4)
//        Util.addNode(head, 5)
//        Util.dumpNode(KList.reverseKGroup(head, 3))


        //二叉树递归遍历
        val head = TreeNode(2)
        head.left = TreeNode(1)
        head.left.left = TreeNode(1)
        head.left.left.right = TreeNode(1)
//        head.right = TreeNode(3)
//        head.left.left = TreeNode(1)
//        head.left.right = TreeNode(3)
//        head.right = TreeNode(6)
//        head.right.right = TreeNode(7)
//        head.left.left = TreeNode(4)
//        head.right.right = TreeNode(5)
//        head.right.left = TNode(6)
//        head.right.right = TNode(7)
//        BinaryTree.preOrder(head)
//        BinaryTree.preOrderItr(head)
//        BinaryTree.midOrder(head)
//        BinaryTree.backOrder(head)
//        BinaryTree.backOrder(head)
//        BinaryTree.backOrderItr(head)
//        BinaryTree.midOrder(head)
//        BinaryTree.midOrderItr(head)
//        BinaryTree.bfsTree(head)

//        BinaryTree.widthOfBinaryTree1(head)
//        val b = BinaryTree.findTargetNode(head, 3)
        var b = BinaryTree().maxDepth(head)
        Log.i("BinaryTree", "maxDepth:${b}")

        b = BinaryTree().minDepth(head)
        Log.i("BinaryTree", "minDepth:${b}")
    }

    @Composable
    fun MainScreen(entries: List<MainEntry>) {
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
                .padding(50.dp)
        ) {
            items(entries) { item: MainEntry ->
                Button(modifier = Modifier
                    .fillMaxWidth()
                    .padding(all = 5.dp), onClick = {
                    val intent = Intent(this@MainActivity, item.target)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(intent)
                }) {
                    Text(text = item.name)
                }
            }
        }
    }
}